<?php
	include('include/header.php');
	include("include/commands.php");
        
        //Check if we are coming to this page from an insert query.
        if ($_POST["fromInsert"] == "true") {
            
            //Instantiate connector.
            $connector = new CloudDBInterface();

            //Add the new service now.
            //Get variables.
            $name = $_POST["name"];
            $rate = $_POST["rate"];
            
            //Create the query.
            $query =    "INSERT INTO Rates (Name, Rate)
                        VALUES (\"$name\", $rate)";

            //Insert the tuple.
            $connector->insertQuery($query);            
        }
        
	$services = getServices();
?>

	<div class="body">
	
		<h1> Services Offered: </h1>
                <div class="border">
                    <form id="newServiceForm" action="services.php" method="post" onsubmit="return validateServiceInsert();">
                        <h4>Add a New Service</h4>
                        <p>
                            Name* <input type="text" name="name" id="name"/><br/>
                            Rate* <input type="text" name="rate" id="rate"/><br/>
                            
                            <input type="hidden" name="fromInsert" id="fromInsert" value="true"/>
                            <input type="submit" value="Submit"/>
                        </p>
                    </form>
                </div>
                <br/>
		<?php 

			if(sizeof($services) > 0){
					generateServiceTable($services);
				}
				else{
					echo "<p> No Service(s) Found. </p>";
				}
				
				function generateServiceTable($services) {

					echo "<table>";
					echo "<tr>";
					echo "<th>#</th>";
					echo "<th>Service Id</th>";
					echo "<th>Name</th>";
					echo "<th>Rate</th>";				
					echo "</tr>";
					$number = 1;
					for ($i=0;$i < sizeof($services); $i++) {
						$row = $services[$i];
						echo "<tr>";
						echo "<td>" .$number ."</td>" ;
						echo "<td>" .$row['Id']."</td>";
						echo "<td>" .$row['Name']."</td>";
						echo "<td>" .$row['Rate']." %</td>";
						echo "</tr>";
						$number++;
					}
					echo "</table>";
				}
		?>
	</div>


<?php 
	include('include/footer.php'); 
?>